package com.wuyb.controller;

import com.wuyb.pojo.entity.BaseResponse;
import com.wuyb.pojo.param.*;
import com.wuyb.service.UserService;
import com.wuyb.util.JwtTokenUtil;
import com.wuyb.util.MyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.Map;

/**
 * 普通用户操作user接口以及基本操作
 *
 * @Author: wyb
 * @Date: 2024/01/08/19:08
 * @Description:
 */
@Api(tags = "普通用户操作user接口及基本操作")
@RestController
@Slf4j
@RequestMapping("/users")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @Resource
    private UserDetailsService userDetailsService;

    @Resource
    private PasswordEncoder passwordEncoder;


    @ApiOperation("登录")
    @PostMapping("/login")
    public BaseResponse<?> login(@RequestBody @Validated UserLoginParam users, HttpServletRequest request) {
        log.info(String.format("post:/users/login，进行登录,%s", users));
        return userService.login(users, request, jwtTokenUtil, userDetailsService, passwordEncoder);
    }

    @ApiOperation("注册")
    @PostMapping("/register")
    public BaseResponse<?> register(@RequestBody @Validated UserRegisterParam users, HttpServletRequest request) {
        log.info(String.format("post:/users/register，进行注册,%s", users));
        return userService.register(users, request, passwordEncoder);
    }

    @ApiOperation(value = "获取当前登录的用户信息")
    @GetMapping("/info")
    public BaseResponse<?> getAdminInfo(Principal principal, HttpServletRequest servletRequest) {
        return userService.getAdminInfo(principal, servletRequest);
    }

    @ApiOperation(value = "修改本人基本信息")
    @PutMapping("/info")
    public BaseResponse<?> updateOwnerInfo(@RequestBody @Validated UserUpdateWithOwnerParam users) {
        return userService.updateOwnerInfo(users);
    }

    @ApiOperation(value = "修改本人密码")
    @PutMapping("/info/password")
    public BaseResponse<?> updateOwnerPass(@RequestBody @Validated UserUpdatePasswordParam users, HttpServletRequest request) {
        return userService.updateOwnerPass(users, passwordEncoder, request);
    }

    @ApiOperation("设置用户头像")
    @PutMapping("/info/headImg")
    public BaseResponse<?> setHeadImg(@Validated UserSetImgParam param) {
        param.setId(MyUtil.getUser().getId());
        log.info(String.format("Put:/users/info/headImg 设置用户头像。%s", param));
        return userService.setHeadImg(param);
    }

    @ApiOperation(value = "通过姓名获取该用户的信息")
    @GetMapping("/info/{userName}")
    public BaseResponse<?> getUserInfoByName(@PathVariable String userName) {
        return userService.getUserInfoByName(userName);
    }

    @ApiOperation(value = "通过uid获取该用户的信息")
    @GetMapping("/info/{uid}/byId")
    public BaseResponse<?> getUserInfoById(@PathVariable Integer uid) {
        return userService.getUserInfoById(uid);
    }

    @ApiOperation(value = "退出登录")
    @PostMapping("/logout")
    public BaseResponse<?> logout() {
        return userService.logout();
    }


    @ApiOperation(value = "签到")
    @PostMapping("/signIn/{mid}")
    public BaseResponse<?> signIn(@RequestBody @Validated ImgBase64Param param, @PathVariable String mid) {
        return userService.signIn(param, mid);
    }


    @ApiOperation(value = "检查是否签到")
    @GetMapping("/signIn/{mid}")
    public BaseResponse<?> getSignIn(@PathVariable String mid) {
        return userService.hadSignIn(mid);
    }


    @ApiOperation(value = "发送好友申请")
    @PostMapping("/friend/application/{userid}")
    public BaseResponse<?> sendFriApplication(@PathVariable Integer userid) {
        return userService.sendFriApplication(userid);
    }

    @ApiOperation(value = "回应好友申请")
    @PostMapping("/friend/application/{userid}/{result}")
    public BaseResponse<?> replyFriApplication(@PathVariable Integer userid, @PathVariable Integer result) {
        return userService.replyFriApplication(userid, result);
    }

    @ApiOperation(value = "查看好友申请列表（未读）")
    @GetMapping("/friend/application/{current}/{size}")
    public BaseResponse<?> getFriApplications(@PathVariable Integer current, @PathVariable Integer size) {
        return userService.getFriApplications(current, size);
    }

    @ApiOperation(value = "根据userid删除好友")
    @DeleteMapping("/friend/{userid}")
    public BaseResponse<?> delFriendById(@PathVariable Integer userid) {
        return userService.delFriendById(userid);
    }

    @ApiOperation(value = "查看好友列表以及聊天记录")
    @GetMapping("/friend/{current}/{size}")
    public BaseResponse<?> getFriendsAndChat(@PathVariable Integer current, @PathVariable Integer size) {
        return userService.getFriendsAndChat(current, size);
    }

    @ApiOperation(value = "查看与好友聊天记录")
    @GetMapping("/friend/{userid}/chat/{current}/{size}")
    public BaseResponse<?> getFriChat(@PathVariable Integer userid, @PathVariable Integer current, @PathVariable Integer size) {
        return userService.getFriChat(userid, current, size);
    }

    @ApiOperation(value = "向好友发送消息")
    @PostMapping("/friend/{userid}/chat")
    public BaseResponse<?> sendFriChat(@PathVariable Integer userid, @RequestBody Map map) {
        String content = (String) map.get("content");
        return userService.sendFriChat(userid, content);
    }

    @ApiOperation(value = "对好友消息进行已读")
    @PutMapping("/friend/{userid}/chat")
    public BaseResponse<?> readFriChat(@PathVariable Integer userid) {
        return userService.readFriChat(userid);
    }


}
